library(survey)
library(dplyr)
library(ggplot2)
library(gridExtra)

# Figure 10.4
summary(factor(df$Q25)) # Transgender ban
summary(factor(df$Q11T)) 

df2 <- df
df2 <- df2[df2$Q11 < 77,]

# Transgender Ban treatments
# P_ASSIGN3 == 1    # Control
# P_ASSIGN3 == 3    # Support
# P_ASSIGN3 == 2    # Oppose
# P_ASSIGN3 == 4    # Divided

# Q25T
df2$Q25T <- NA
df2$Q25T[df2$Q25 < 3] <- 1
df2$Q25T[df2$Q25 > 2 & df2$Q25 < 77] <- 0

# Survey designs
w2_design <-
  svydesign(
    id = ~ 1,
    weights = ~ weight2,
    data = df2
  )


# Transgender ban
tban <- svyby(~Q25T,~P_ASSIGN3+party+Q11T, w2_design, svymean, na.rm = TRUE)

# Build out plots
# TBan
pt.est <- c(tban$Q25T[tban$P_ASSIGN3==1& tban$Q11T==0&tban$party==0], 
            tban$Q25T[tban$P_ASSIGN3==3& tban$Q11T==0&tban$party==0],
            tban$Q25T[tban$P_ASSIGN3==2& tban$Q11T==0&tban$party==0],
            tban$Q25T[tban$P_ASSIGN3==4& tban$Q11T==0&tban$party==0],
            0,
            tban$Q25T[tban$P_ASSIGN3==1& tban$Q11T==0&tban$party==2], 
            tban$Q25T[tban$P_ASSIGN3==3& tban$Q11T==0&tban$party==2],
            tban$Q25T[tban$P_ASSIGN3==2& tban$Q11T==0&tban$party==2],
            tban$Q25T[tban$P_ASSIGN3==4& tban$Q11T==0&tban$party==2],
            0,
            tban$Q25T[tban$P_ASSIGN3==1& tban$Q11T==1&tban$party==0], 
            tban$Q25T[tban$P_ASSIGN3==3& tban$Q11T==1&tban$party==0],
            tban$Q25T[tban$P_ASSIGN3==2& tban$Q11T==1&tban$party==0],
            tban$Q25T[tban$P_ASSIGN3==4& tban$Q11T==1&tban$party==0],
            0,
            tban$Q25T[tban$P_ASSIGN3==1& tban$Q11T==1&tban$party==2], 
            tban$Q25T[tban$P_ASSIGN3==3& tban$Q11T==1&tban$party==2],
            tban$Q25T[tban$P_ASSIGN3==2& tban$Q11T==1&tban$party==2],
            tban$Q25T[tban$P_ASSIGN3==4& tban$Q11T==1&tban$party==2])
ci.low <- c(tban$Q25T[tban$P_ASSIGN3==1& tban$Q11T==0&tban$party==0]-1.96*tban$se[tban$P_ASSIGN3==1& tban$Q11T==0&tban$party==0], 
            tban$Q25T[tban$P_ASSIGN3==3& tban$Q11T==0&tban$party==0]-1.96*tban$se[tban$P_ASSIGN3==3& tban$Q11T==0&tban$party==0],
            tban$Q25T[tban$P_ASSIGN3==2& tban$Q11T==0&tban$party==0]-1.96*tban$se[tban$P_ASSIGN3==2& tban$Q11T==0&tban$party==0],
            tban$Q25T[tban$P_ASSIGN3==4& tban$Q11T==0&tban$party==0]-1.96*tban$se[tban$P_ASSIGN3==4& tban$Q11T==0&tban$party==0],0,
            tban$Q25T[tban$P_ASSIGN3==1& tban$Q11T==0&tban$party==2]-1.96*tban$se[tban$P_ASSIGN3==1& tban$Q11T==0&tban$party==2], 
            tban$Q25T[tban$P_ASSIGN3==3& tban$Q11T==0&tban$party==2]-1.96*tban$se[tban$P_ASSIGN3==3& tban$Q11T==0&tban$party==2],
            tban$Q25T[tban$P_ASSIGN3==2& tban$Q11T==0&tban$party==2]-1.96*tban$se[tban$P_ASSIGN3==2& tban$Q11T==0&tban$party==2],
            tban$Q25T[tban$P_ASSIGN3==4& tban$Q11T==0&tban$party==2]-1.96*tban$se[tban$P_ASSIGN3==4& tban$Q11T==0&tban$party==2],0,
            tban$Q25T[tban$P_ASSIGN3==1& tban$Q11T==1&tban$party==0]-1.96*tban$se[tban$P_ASSIGN3==1& tban$Q11T==1&tban$party==0], 
            tban$Q25T[tban$P_ASSIGN3==3& tban$Q11T==1&tban$party==0]-1.96*tban$se[tban$P_ASSIGN3==3& tban$Q11T==1&tban$party==0],
            tban$Q25T[tban$P_ASSIGN3==2& tban$Q11T==1&tban$party==0]-1.96*tban$se[tban$P_ASSIGN3==2& tban$Q11T==1&tban$party==0],
            tban$Q25T[tban$P_ASSIGN3==4& tban$Q11T==1&tban$party==0]-1.96*tban$se[tban$P_ASSIGN3==4& tban$Q11T==1&tban$party==0],0,
            tban$Q25T[tban$P_ASSIGN3==1& tban$Q11T==1&tban$party==2]-1.96*tban$se[tban$P_ASSIGN3==1& tban$Q11T==1&tban$party==2], 
            tban$Q25T[tban$P_ASSIGN3==3& tban$Q11T==1&tban$party==2]-1.96*tban$se[tban$P_ASSIGN3==3& tban$Q11T==1&tban$party==2],
            tban$Q25T[tban$P_ASSIGN3==2& tban$Q11T==1&tban$party==2]-1.96*tban$se[tban$P_ASSIGN3==2& tban$Q11T==1&tban$party==2],
            tban$Q25T[tban$P_ASSIGN3==4& tban$Q11T==1&tban$party==2]-1.96*tban$se[tban$P_ASSIGN3==4& tban$Q11T==1&tban$party==2])
ci.high <- c(tban$Q25T[tban$P_ASSIGN3==1& tban$Q11T==0&tban$party==0]+1.96*tban$se[tban$P_ASSIGN3==1& tban$Q11T==0&tban$party==0], 
             tban$Q25T[tban$P_ASSIGN3==3& tban$Q11T==0&tban$party==0]+1.96*tban$se[tban$P_ASSIGN3==3& tban$Q11T==0&tban$party==0],
             tban$Q25T[tban$P_ASSIGN3==2& tban$Q11T==0&tban$party==0]+1.96*tban$se[tban$P_ASSIGN3==2& tban$Q11T==0&tban$party==0],
             tban$Q25T[tban$P_ASSIGN3==4& tban$Q11T==0&tban$party==0]+1.96*tban$se[tban$P_ASSIGN3==4& tban$Q11T==0&tban$party==0],0,
             tban$Q25T[tban$P_ASSIGN3==1& tban$Q11T==0&tban$party==2]+1.96*tban$se[tban$P_ASSIGN3==1& tban$Q11T==0&tban$party==2], 
             tban$Q25T[tban$P_ASSIGN3==3& tban$Q11T==0&tban$party==2]+1.96*tban$se[tban$P_ASSIGN3==3& tban$Q11T==0&tban$party==2],
             tban$Q25T[tban$P_ASSIGN3==2& tban$Q11T==0&tban$party==2]+1.96*tban$se[tban$P_ASSIGN3==2& tban$Q11T==0&tban$party==2],
             tban$Q25T[tban$P_ASSIGN3==4& tban$Q11T==0&tban$party==2]+1.96*tban$se[tban$P_ASSIGN3==4& tban$Q11T==0&tban$party==2],0,
             tban$Q25T[tban$P_ASSIGN3==1& tban$Q11T==1&tban$party==0]+1.96*tban$se[tban$P_ASSIGN3==1& tban$Q11T==1&tban$party==0], 
             tban$Q25T[tban$P_ASSIGN3==3& tban$Q11T==1&tban$party==0]+1.96*tban$se[tban$P_ASSIGN3==3& tban$Q11T==1&tban$party==0],
             tban$Q25T[tban$P_ASSIGN3==2& tban$Q11T==1&tban$party==0]+1.96*tban$se[tban$P_ASSIGN3==2& tban$Q11T==1&tban$party==0],
             tban$Q25T[tban$P_ASSIGN3==4& tban$Q11T==1&tban$party==0]+1.96*tban$se[tban$P_ASSIGN3==4& tban$Q11T==1&tban$party==0],0,
             tban$Q25T[tban$P_ASSIGN3==1& tban$Q11T==1&tban$party==2]+1.96*tban$se[tban$P_ASSIGN3==1& tban$Q11T==1&tban$party==2], 
             tban$Q25T[tban$P_ASSIGN3==3& tban$Q11T==1&tban$party==2]+1.96*tban$se[tban$P_ASSIGN3==3& tban$Q11T==1&tban$party==2],
             tban$Q25T[tban$P_ASSIGN3==2& tban$Q11T==1&tban$party==2]+1.96*tban$se[tban$P_ASSIGN3==2& tban$Q11T==1&tban$party==2],
             tban$Q25T[tban$P_ASSIGN3==4& tban$Q11T==1&tban$party==2]+1.96*tban$se[tban$P_ASSIGN3==4& tban$Q11T==1&tban$party==2])
plot.labs <- c("Control","Support","Oppose","Divided","",
               "Control ","Support ","Oppose ","Divided "," ",
               " Control"," Support"," Oppose"," Divided","  ",
               " Control "," Support "," Oppose "," Divided ")
party <- c("D","D","D","D","NA","R","R","R","R","NA",
           "D","D","D","D","NA","R","R","R","R")

df.plot <- data.frame(Treatment=plot.labs, estimate=pt.est, 
                      lower=ci.low, upper=ci.high,party=party)
df.plot$Treatment <- factor(df.plot$Treatment, levels = plot.labs)

g.tbp <- ggplot(data=df.plot, 
              aes(x=Treatment, y=estimate),
              size = 0.5)+
  geom_col(position=position_dodge(1), width=0.75,
           fill=c("blue4","blue4",'blue4','blue4','black',"red4","red4","red4","red4","black",
                  "blue4","blue4",'blue4','blue4','black',"red4","red4","red4","red4"))+
  geom_errorbar(aes(ymin=lower,ymax=upper),width=0.2)+
  ylim(0,.8)+
  ylab("Respondent Agreement (%)")+
  xlab("Not Confident                                                                                                             Confident    ")+
  ggtitle("")+
  geom_text(aes(label = round(estimate,3)), vjust = -0.5)

g.tbp
